package com.xxg.renrenshoppingcart.controller;

import com.xxg.renrenshoppingcart.domain.query.AddBatchProductQuery;
import com.xxg.renrenshoppingcart.domain.query.AddProductQuery;
import com.xxg.renrenshoppingcart.domain.query.RemoveProductQuery;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenshoppingcart.domain.result.CartProductResult;
import com.xxg.renrenshoppingcart.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车controller
 *
 * @author xxg
 * @date 2020/11/13
 */
@Api(tags = "购物车管理")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    private final ShoppingCartService shoppingCartService;

    public ShoppingCartController(ShoppingCartService shoppingCartService) {
        this.shoppingCartService = shoppingCartService;
    }

    @ApiOperation("添加商品到购物车")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "商品和数量", required = true, dataType = "AddProductQuery")})
    public BaseResult<Void> add(@RequestBody @Validated AddProductQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.addProduct(userId, query);
    }

    @ApiOperation("批量添加多个商品到购物车")
    @RequestMapping(value = "addBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "多件商品和数量", required = true, dataType = "AddBatchProductQuery")})
    public BaseResult<Void> addBatch(@RequestBody @Validated AddBatchProductQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.addBatch(userId, query);
    }

    @ApiOperation("编辑购物车中商品数量")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "商品和数量", required = true, dataType = "AddProductQuery")})
    public BaseResult<Void> edit(@RequestBody @Validated AddProductQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.updateProductQuantity(userId, query);
    }

    @ApiOperation("将商品从购物车移除")
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "商品ID", required = true, dataType = "RemoveProductQuery")})
    public BaseResult<Void> remove(@RequestBody @Validated RemoveProductQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.removeProduct(userId, query);
    }

    @ApiOperation("清空购物车")
    @RequestMapping(value = "clear", method = RequestMethod.GET)
    public BaseResult<Void> clear(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.removeAllProduct(userId);
    }

    @ApiOperation("查询购物车")
    @RequestMapping(value = "query", method = RequestMethod.GET)
    public BaseResult<CartProductResult> query(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return shoppingCartService.queryProducts(userId);
    }
}
